package guda.mrpc.core.transport.tcp;

import guda.mrpc.core.codec.RpcDecoder;
import guda.mrpc.core.codec.RpcEncoder;
import guda.mrpc.core.serialize.ObjectSerialize;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * Created by well on 17/5/23.
 */
public class TcpClientChannelInitializer extends ChannelInitializer<SocketChannel> {


    private ObjectSerialize objectSerialize;


    public TcpClientChannelInitializer(ObjectSerialize objectSerialize){
        this.objectSerialize=objectSerialize;
    }

    @Override
    protected void initChannel(SocketChannel sc) throws Exception {
        sc.pipeline()
                .addLast(new IdleStateHandler(15,0,0, TimeUnit.SECONDS))
                .addLast(new RpcEncoder(objectSerialize))
                .addLast(new RpcDecoder(objectSerialize))
                .addLast(new TcpClientHandler());
    }
}
